package com.su.volley;

import java.util.concurrent.PriorityBlockingQueue;

public class NetWorkDispater extends Thread {
	private RequestQueue mRequestQueue;
	private PriorityBlockingQueue<IRequest<?>> mIRequests;

	private boolean mQuit = false;

	public NetWorkDispater(RequestQueue requestQueue,
			PriorityBlockingQueue<IRequest<?>> mWorkTasks) {
		this.mRequestQueue = requestQueue;
		this.mIRequests = mWorkTasks;
	}

	@Override
	public void run() {
		while (true) {

			IRequest<?> request = null;
			try {
				// 这里是一个阻塞的实现,如果为空的时候就去阻塞
				request = mIRequests.poll();
			} finally {
				if (isQuit()) {
					return;
				}
			}
			try {
				if (request != null) {
					request.request();
				}
			} finally {

			}
		}

	}

	public boolean isQuit() {
		return mQuit;
	}

	public void setQuit(boolean quit) {
		mQuit = quit;
	}
	
	public void quit(){
		mQuit = true;
		interrupt();
	}

}
